import Vue from 'vue'
import VueRouter from 'vue-router'
import { notification } from 'ant-design-vue'
let whiteList = ['/Login', '/404', '/visionTest01', '', '/']
let whiteList1 = ['/Login', '/404', '/visionTest01', '', '/', '/ChooseClass', '/Setting']
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/Login',
    component: () => import('@/views/login/Login.vue')
  },
  {
    path: '/ClassList',
    component: () => import('@/views/login/ClassList.vue')
  },//班级列表
  {
    path: '/ChoosePlan',
    component: () => import('@/views/login/ChoosePlan.vue')
  },//选择计划
  {
    path: '/ChooseClass',
    component: () => import('@/views/login/ChooseClass.vue')
  },//选择班级
  {
    path: '/ChooseEye',
    name: 'ChooseEye',
    component: () => import('@/views/vision-test/ChooseEye.vue')
  },//选择左眼/右眼
  {
    path: '/VisionTest',
    name: 'VisionTest',
    component: () => import('@/views/vision-test/VisionTest.vue')
  },//视力测试
  {
    path: '/TestResult',
    name: 'TestResult',
    component: () => import('@/views/vision-test/TestResult.vue')
  },//测试结果
  {
    path: '/Setting',
    name: 'Setting',
    component: () => import('@/views/login/Setting.vue')
  },//设置
  {
    path: '/TestsComplete',
    name: 'TestsComplete',
    component: () => import('@/views/vision-test/TestsComplete.vue')
  },//测试完成
]

const router = new VueRouter({
  mode: 'hash',
  routes
})


// 添加路由守卫，防止未登录用户访问受保护页面
router.beforeEach((to, from, next) => {
  console.log(to, 'toto')
  console.log(from, 'fromfrom')
  const myUserInfo = sessionStorage.getItem('myUserInfo');  // 获取本地存储的用户信息
  const sizeValue = localStorage.getItem('sizeValue');  // 视标参数
  // 如果用户未登录且试图访问登录页面以外的页面，则重定向到登录页面
  let loginIndex = whiteList.findIndex(i => i == to.path)
  let sizeValueIndex = whiteList1.findIndex(i => i == to.path)
  console.log(sizeValueIndex, 'sizeValueIndex')
  console.log(sizeValue, 'sizeValue')
  if (loginIndex == -1) {
    if (!myUserInfo) {
      sessionStorage.clear()
      notification.error({
        message: '提示',
        description: '账号未登录，请先进行登录！'
      })
      next({ path: '/Login' });
    } else if (!sizeValue && sizeValueIndex == -1) {
      notification.error({
        message: '提示',
        description: '视标参数未设置，请前往设置'
      })
      next({ path: from.path });
    }
    else {
      next();  // 有权限,继续访问目标页面
    }
  } else {
    next();  // 如果用户已登录，继续访问目标页面
  }
});

export default router
